//Documentation complete
/**
 * @author Kazu
 * @version 2009MR22
 */

package broker;

import problemDomain.*;
import static problemDomain.ErrorBean.*;
import static problemDomain.ErrorBean.ErrorCode.*;

import java.sql.*;
import java.util.*;

/**
 * Database broker for menu item report.
 */
public class MenuItemReportBroker extends ReportBroker {
	
	/* Singleton Constructor */

	private static MenuItemReportBroker broker;

	/**
	 * Constructs a <code>MenuItemReportBroker</code>.
	 * @throws ErrorBean
	 */
	private MenuItemReportBroker () throws ErrorBean {}

	/**
	 * Retrieves the static FoodItemBroker instance.
	 * @return static FoodItemBroker
	 * @throws ErrorBean
	 */
	public synchronized static MenuItemReportBroker getBroker () throws ErrorBean {
		if (broker == null)
			broker = new MenuItemReportBroker();
		return broker;
	}

	/* END Singleton Constructor */
	
	
	/* Methods */
	
	/**
	 * Generates the report.
	 * @return a <code>List</code> of the results
	 * @throws ErrorBean if there is an error retrieving the data
	 */
	public List<MenuItemReport> report () throws ErrorBean {
		List<MenuItemReport> search = new ArrayList<MenuItemReport>();
		MenuItemReport icpr;
		String sql = "SELECT menu_item_id, mi.name, cost, price FROM menu_item mi "
			+ "JOIN menu_item_list ON (menu_item_id = fk_menu_item_id) "
			+ "JOIN food_item fi ON (fk_food_item_id = food_item_id) "
			+ "WHERE mi.active = TRUE AND fi.active = TRUE";

		try {
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sql);

			while (rs.next()) {
				icpr = new MenuItemReport();
				icpr.setId(rs.getInt("menu_item_id"));
				icpr.setName(rs.getString("name"));
				icpr.setCost(rs.getDouble("cost"));
				//icpr.setPrice(rs.getDouble("price"));
				search.add(icpr);
			}

		} catch (SQLException e) {
			throwError(
				BROKER_SELECT_ERROR,
				e.getMessage(),
				this);
		}
		return search;
	}
	
	/* END Methods */
	
}
